package com.luke.exercise.jndi;

import java.rmi.registry.LocateRegistry;

import javax.naming.Context;
import javax.naming.InitialContext;

public class Test {
    
    public static void main(String[] args) throws Exception {
        initPeson();
        findPerson();
    }

    private static void findPerson() throws Exception {
        InitialContext ctx = new InitialContext();
        Person person = (Person) ctx.lookup("person");
        System.out.println(person.toString());
        ctx.close();
    }

    private static void initPeson() throws Exception {
        LocateRegistry.createRegistry(3001);
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
        System.setProperty(Context.PROVIDER_URL,"rmi://localhost:3001");
        InitialContext ctx = new InitialContext();
        
        Person p  = new Person();
        p.setName("hello");
        p.setPassword("jndi");
        
        ctx.bind("person", p);
        ctx.close();
    }

}
